package com.guozcc.bootweb.platform.config.mybatis.autointercept;


import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @desc MyBatis自定义ResultSetHandler拦截器实现
 * @功能：修改结果，String类型，脱敏成***
 * @Created by guozcc
 * @Data 20230914
 */
@Intercepts({
        @Signature(type= ResultSetHandler.class,method = "handleResultSets",args = {Statement.class})})
public class AutoResultSetHandlerInterceptor implements Interceptor {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object proceed = invocation.proceed();
        this.printRes(proceed);
        return proceed;
    }

    private void printRes(Object proceed){
        try {
            String res = null;
            if(proceed instanceof List){
                res = "size="+((List) proceed).size();
            }else if(proceed instanceof Map){
                res = "" +proceed.toString();
            }else if(proceed instanceof JSONObject){
                res = "" +((JSONObject)proceed).toJSONString();
            }else if(proceed instanceof Long || proceed instanceof Integer|| proceed instanceof String){
                res = ""+proceed;
            }else {
                res = proceed.toString();
            }
            log.info("结果:"+res);
        }catch (Exception e){
            log.error("",e);
        }
    }
}
